import express from 'express';
import {
  CopilotRuntime,
  ExperimentalEmptyAdapter,
  copilotRuntimeNodeHttpEndpoint,
} from '@copilotkit/runtime';

const app = express();

const serviceAdapter = new ExperimentalEmptyAdapter();

app.use('/api/copilotkit', (req, res, next) => {
  (async () => {
    console.log(`[${new Date().toISOString()}] CopilotKit request: ${req.method} ${req.url}`);
    const runtime = new CopilotRuntime({
      remoteEndpoints: [
        {
          url: "http://10.192.72.11:7000/agui/copilotkit",
        },
      ],
    });
    const handler = copilotRuntimeNodeHttpEndpoint({
      endpoint: '/api/copilotkit',
      runtime,
      serviceAdapter,
    });

    return handler(req, res);
  })().catch(next);
});

const port = 4000;
app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});